package holder;

import java.util.List;

import com.example.googlemarketdemo.R;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.nineoldandroids.animation.ObjectAnimator;
import com.nineoldandroids.animation.ValueAnimator;
import com.nineoldandroids.animation.ValueAnimator.AnimatorUpdateListener;

import conf.constants.URLS;

import utils.BitmapHepler;
import utils.UIUtils;


import android.view.View;
import android.view.View.MeasureSpec;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import base.BaseHolder;
import bean.AppInfoBean;
import bean.AppInfoBean.AppinfoSafeBean;
/**
 * 安全的holder
 * @author Anson-pc
 *
 */
public class AppDetailSafeHolder extends BaseHolder<AppInfoBean> implements OnClickListener {
	
	private TextView mTv;
	/*-----------  begin ----------*/
	@ViewInject(R.id.app_detail_safe_iv_arrow)
	ImageView mIvArrow;
	@ViewInject(R.id.app_detail_safe_des_container)
	LinearLayout mContainerDes;
	@ViewInject(R.id.app_detail_safe_pic_container)
	LinearLayout mContainerPic;
	
	/*--------- end ----------*/

	//定义动画的boolean值、判断是否打开或者关闭
	private boolean isOpen = true;
	
	@Override
	protected View initHolderView() {
		View view = View.inflate(UIUtils.getContext(), R.layout.item_detail_safe, null);
		//设置点击事件
		view.setOnClickListener(this);
		//注解
		ViewUtils.inject(this,view);
		return view;
	}

	@Override
	protected void refreshHolderView(AppInfoBean data) {
		List<AppinfoSafeBean> safes = data.safe;
		//遍历这个集合
		for (int i = 0; i < safes.size(); i++) {
			AppinfoSafeBean appinfoSafeBean = safes.get(i);
			//绑定数据
			ImageView ivPic = new ImageView(UIUtils.getContext());
			//加载图片
			BitmapHepler.display(ivPic, URLS.IMAGEBASEURL+appinfoSafeBean.safeUrl);
			mContainerPic.addView(ivPic);
			
			//添加文本
			LinearLayout line = new LinearLayout(UIUtils.getContext());
			ImageView ivDesIcon = new ImageView(UIUtils.getContext());
			TextView tvDesNote = new TextView(UIUtils.getContext());
			
			//设置间距
			int padding = UIUtils.dip2px(4);
			line.setPadding(padding, padding, padding, padding);
			
			//赋值
			BitmapHepler.display(ivDesIcon, URLS.IMAGEBASEURL+appinfoSafeBean.safeDesUrl);
			//显示TextView的内容
			tvDesNote.setText(appinfoSafeBean.safeDes);
			//设置文本的颜色
			int safeDesColor = appinfoSafeBean.safeDesColor;
			/**
			 * 判断颜色的值，为0 则设置为普通的颜色，不为0 则显示警告的颜色
			 *
			 */
			if (safeDesColor == 0) {
				tvDesNote.setTextColor(UIUtils.getColor(R.color.app_detail_safe_normal));//获取普通的颜色
			}else{
				tvDesNote.setTextColor(UIUtils.getColor(R.color.app_detail_safe_warning));//获取警告的颜色
			}
			
			line.addView(ivDesIcon);
			line.addView(tvDesNote);
			
			mContainerDes.addView(line);
			
		}
		
		//默认的折叠
		toggleAnimation(false);
	}
	
	/**
	 * 点击事件
	 */
	@Override
	public void onClick(View v) {
		toggleAnimation(true);
	}

	public void toggleAnimation(boolean isAnimation) {
		if (isOpen) {//打开->折叠 mContainerDes应有的高度到0的过程
			mContainerDes.measure(0, 0);
			/*int widthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
			int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
			mContainerDes.measure(widthMeasureSpec, heightMeasureSpec);*/
			int measureHeight = mContainerDes.getHeight();
			int start = measureHeight ;
			int end = 0;
			if (isAnimation) {
				doAnimation(start, end);
			} else {
				LayoutParams params = mContainerDes.getLayoutParams();
				params.height = end;
				mContainerDes.setLayoutParams(params);
			}
			
		} else {//折叠 ->打开 应有的高度到0的过程
			mContainerDes.measure(0, 0);
			//获取测量的高度
			int measureHeight  = mContainerDes.getMeasuredHeight();
			
			int start = 0;
			int end = measureHeight;
			
			if (isAnimation) {
				doAnimation(start, end);
				
			} else {
				LayoutParams params = mContainerDes.getLayoutParams();
				params.height = end;
				mContainerDes.setLayoutParams(params);
			}
			
		}
		
		isOpen = !isOpen;
	}

	private void doAnimation(int start, int end) {
		ValueAnimator animator = ValueAnimator.ofInt(start,end);
		animator.start();
		
		//1.得到动画中的渐变值，对动画进行监听
		animator.addUpdateListener(new AnimatorUpdateListener() {
			
			@Override
			public void onAnimationUpdate(ValueAnimator value) {
				int height = (Integer) value.getAnimatedValue();
				
				LayoutParams params = mContainerDes.getLayoutParams();
				params.height = height;
				mContainerDes.setLayoutParams(params);
			}
		});
		
		
		//箭头的旋转动画
		if (isOpen) {
			ObjectAnimator.ofFloat(mIvArrow, "rotation", 0,180).start();
		}else{
			ObjectAnimator.ofFloat(mIvArrow, "rotation", 180,0).start();
		}
	
	}
	
}
